﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using housingsafety.server.Models;

namespace housingsafety.server
{
    public static class GeoServerBuilderExtensions
    {
        //public static IApplicationBuilder UseGeoServerProxy(this IApplicationBuilder app)
        //{
        //    IHttpForwarder forwarder = (IHttpForwarder)app.ApplicationServices.GetService(typeof(IHttpForwarder));
        //    var geoServerConfig = (IOptions<GeoServerConfig>)app.ApplicationServices.GetService(typeof(IOptions<GeoServerConfig>));
        //    var httpClient = new HttpMessageInvoker(new SocketsHttpHandler()
        //    {
        //        UseProxy = false,
        //        AllowAutoRedirect = false,
        //        AutomaticDecompression = DecompressionMethods.None,
        //        UseCookies = false,
        //        Credentials = null

        //    });

        //    var config = geoServerConfig.Value;
        //    var transformer = new CustomTransformer(); // or HttpTransformer.Default;
        //    var requestOptions = new ForwarderRequestConfig ();

        //    app.UseEndpoints(endpoints =>
        //    {
        //        endpoints.Map("/{**catch-all}", async httpContext =>
        //        {
        //            //httpContext.Request.
        //            var error = await forwarder.SendAsync(httpContext, $"https://{config.host}:{config.port}/",
        //                httpClient, requestOptions, transformer);
        //            // Check if the operation was successful
        //            if (error != ForwarderError.None)
        //            {
        //                var errorFeature = httpContext.GetForwarderErrorFeature();
        //                var exception = errorFeature.Exception;
        //            }
        //        });
        //    });
        //    return app;
        //}

        //private class CustomTransformer : HttpTransformer
        //{
        //    public override async ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix)
        //    {
        //        // Copy all request headers
        //        await base.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix);

        //        // Customize the query string:
        //        //var queryContext = new QueryTransformContext(httpContext.Request);
        //        //queryContext.Collection.Remove("param1");
        //        //queryContext.Collection["area"] = "xx2";

        //        //// Assign the custom uri. Be careful about extra slashes when concatenating here.
        //        //proxyRequest.RequestUri = new Uri(destinationPrefix + httpContext.Request.Path + queryContext.QueryString);

        //        // Suppress the original request header, use the one from the destination Uri.
        //        proxyRequest.Headers.Host = null;
        //    }
        //}
        public static IApplicationBuilder UseGeoServerProxy(this IApplicationBuilder app)
        {
            
            return app;
        }
    }
}
